package com.eric.windows.imgDispose;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.PersistableBundle;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;

import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

@EActivity
public class PinBallActivity extends Activity {
    private int tableWidth;
    private int tableHeight;
    private int racketY;
    private final  int RACKET_HEIGHT = 20;
    private final  int RACKET_WIDTH = 20;
    private final int BALL_SIZE = 12;
    private int ySpeed = 10;
    Random rand = new Random();
    private double xyRate = rand.nextDouble() - 0.5;
    private int xSpeed = (int) ( ySpeed * xyRate * 2 );
    private int ballX = rand.nextInt(200) + 20;
    private int ballY = rand.nextInt(10) + 20;
    private int racketX = rand.nextInt(200);
    public boolean isCLose = false;
    GameView gameView;

    @Override
    public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        gameView = new GameView(this);
        gameView.setBackgroundColor(Color.BLACK);
        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        gameView.setLayoutParams(layoutParams);
        setContentView(gameView);
    }

    @AfterViews
    void initData(){

        WindowManager windowManager  =  getWindowManager();
        Display  display  = windowManager.getDefaultDisplay();
        DisplayMetrics metrics = new DisplayMetrics();
        display.getMetrics(metrics);
        tableHeight  = metrics.widthPixels;
        tableWidth = metrics.heightPixels;

        racketY = tableHeight -80;
        final Handler handler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if (msg.what == 0x123){
                    gameView.invalidate();
                }
            }
        };

        gameView.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                switch (event.getKeyCode()){
                    case KeyEvent.KEYCODE_A:
                        if (racketX > 0){
                            racketX -= 10;
                        }
                        break;
                    case KeyEvent.KEYCODE_D:
                        if (racketX < tableWidth - RACKET_WIDTH){
                            racketX += 10;
                        }
                        break;
                }
                gameView.invalidate();
                return true;
            }
        });

        final Timer  timer  = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                //如果小球碰到左边边框
                if (ballX <= 0 || ballX >= tableWidth - BALL_SIZE){
                    xSpeed = -xSpeed;
                }
                //如果小球高度超出了球拍位置，且横向不在球拍范围之内，游戏结束
                if (ballY >= racketY - BALL_SIZE && (ballX < racketX || ballX > racketX + RACKET_WIDTH)){
                    timer.cancel();
                    isCLose = true;
                    //如果小球位于球拍之内，且到达球拍位置，小球反弹
                }else if (ballY <= 0 || (ballY >= racketY - BALL_SIZE && ballX > racketX && ballX <= racketX + RACKET_WIDTH)){
                    ySpeed = -ySpeed;
                }

                ballY += ySpeed;
                ballX += xSpeed;
                handler.sendEmptyMessage(0x123);
            }
        },0,100);
    }

    public class GameView extends View {

        Paint paint = new Paint();
        public GameView(Context context) {
            super(context);
            initData();
        }

        public GameView(Context context, AttributeSet attrs) {
            super(context, attrs);
            initData();

        }

        public GameView(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            initData();

        }

        @TargetApi(Build.VERSION_CODES.LOLLIPOP)
        public GameView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
            super(context, attrs, defStyleAttr, defStyleRes);
            initData();
        }

        void initData(){
            paint.setAntiAlias(true);
        }

        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            if (isCLose){
                paint.setColor(Color.RED);
                paint.setTextSize(40);
                canvas.drawText("游戏结束",50,200,paint);
            }else {
                paint.setColor(Color.rgb(240,240,80));
                canvas.drawCircle(ballX,ballY,BALL_SIZE,paint);
                paint.setColor(Color.rgb(80,80,200));
                canvas.drawRect(racketX,racketY,racketX + RACKET_WIDTH,racketY + RACKET_HEIGHT,paint);
            }
        }

    }


}
